/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.object.Comment;
import etcore.webapp.grouptalk.common.service.CommentService;
import etcore.webapp.grouptalk.common.service.MessageService;
import etcore.webapp.grouptalk.user.object.User;

/**
 * This action validates the request and executes the command of getting my
 * messages,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class MyCommentsAction extends AbstractAction {

	private static final long serialVersionUID = 4599787097897244199L;
	// validate result
	public static final int ACCESS_ALLOWED = 0;
	public static final int ERROR = 1;
	public static final int REMOVE_ALLOWED = 2;
	// utility constants

	public static final String START_PID = "startpid";
	public static final String END_PID = "endpid";
	public static final String COMMENT_NUM = "commentNum";
	public static final String LAST_COMMENT = "lastComment";
	public static final String REMOVE_COMMENT = "removeComment";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		User sessionUser = (User) session.getAttribute(Messages.SESSION_USER);
		String cmd = request.getParameter(PageConstants.CMD);
		if (sessionUser != null && REMOVE_COMMENT.equals(cmd))
			return REMOVE_ALLOWED;
		else if (sessionUser != null)
			return ACCESS_ALLOWED;
		return ERROR;

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		if (ERROR == validationResult)
			return PageConstants.ACTION_PATH_ERROR;
		else if (REMOVE_ALLOWED == validationResult )
			 return PageConstants.PAGE_ACTION_MYINFO + "?init=myComments";
		return PageConstants.PAGE_PATH_MYCOMMENTS;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		if (REMOVE_ALLOWED == validationResult)
			removeComment(request);
		if (ACCESS_ALLOWED == validationResult
				|| REMOVE_ALLOWED == validationResult) {
			getComments(request, response);
		}

	}

	private void removeComment(HttpServletRequest request) {
		String commentId = request.getParameter(PageConstants.ID);

		HttpSession session = request.getSession(true);
		User sessionUser = (User) session.getAttribute(Messages.SESSION_USER);

		if (sessionUser != null && commentId != null) {
			CommentService commentService = new CommentService();
			Comment comment = commentService.findById(new Integer(commentId));
			if (comment != null
					&& comment.getUserName().equals(sessionUser.getUserName()))
				commentService.deleteSingle(comment);
		}
	}

 
	private void getComments(HttpServletRequest request,
			HttpServletResponse response) {
		int intpageid = 1;

		HttpSession session = request.getSession(true);
		User sessionUser = (User) session.getAttribute(Messages.SESSION_USER);
		CommentService commentService = new CommentService();

		int unit = 10;

		List<Comment> comments = null;
		long all = 0;
		int begin = 0, pagenums = 0;
		String pageid = request.getParameter(PageConstants.PAGEID);

		if (pageid != null)
			intpageid = Integer.parseInt(pageid);

		all = commentService.findSizeByProperty(PageConstants.USERNAME,
				sessionUser.getUserName());

		pagenums = (int) ((all + unit - 1) / unit);
		begin = unit * (intpageid - 1);
		MessageService messageService = new MessageService();
		comments = commentService.findByProperty(PageConstants.USERNAME,
				sessionUser.getUserName(), begin, unit);
		if (comments == null)
			return;
		for (Comment comment : comments) {
			@SuppressWarnings("rawtypes")
			List msgs=messageService.findByProperty(PageConstants.ID, comment.getGmid(), 0, 1);
			if(msgs!=null&&msgs.size()>0){
			Object[] msg=(Object[])msgs.get(0);
			request.setAttribute(comment.getId()+PageConstants.MSG, msg);
			}
		}
		int startpid = intpageid - 3;
		if (startpid <= 0)
			startpid = 1;
		int endpid = intpageid + 8;
		if (intpageid > 8)
			endpid = intpageid + 6;
		if (endpid >= pagenums)
			endpid = pagenums;

		request.setAttribute(START_PID, startpid);
		request.setAttribute(END_PID, endpid);

		request.setAttribute(PageConstants.COMMENTS, comments);
		request.setAttribute(PageConstants.PAGENUMS, pagenums);
		request.setAttribute(PageConstants.ALL, all);
		request.setAttribute(PageConstants.UNIT, unit);
		request.setAttribute(PageConstants.INTPAGEID, intpageid);

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#
	 * buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
